package com.xhh.nphm.system.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhh.nphm.common.core.domain.QueryConditionVo;
import com.xhh.nphm.common.core.domain.entity.SysUser;
import com.xhh.nphm.common.utils.MapDataUtil;
import com.xhh.nphm.framework.config.ReflectUtil;
import com.xhh.nphm.system.domain.MWorkcsbase;
import com.xhh.nphm.system.domain.MailExcel;
import com.xhh.nphm.system.domain.MmailWorkForm;
import com.xhh.nphm.system.domain.TWorkcsbase;
import com.xhh.nphm.system.mapper.CommonDao;
import com.xhh.nphm.system.mapper.DataExportDao;
import com.xhh.nphm.system.service.MmailWorkFormService;
import com.xhh.nphm.system.mapper.MmailWorkFormDao;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 信件工单业务实现类
* @author Administrator
* @description 针对表【mMailWorkForm】的数据库操作Service实现
* @createDate 2022-06-07 15:41:25
*/
@Service
public class MmailWorkFormServiceImpl extends ServiceImpl<MmailWorkFormDao, MmailWorkForm>
    implements MmailWorkFormService{

    @Autowired
    private MmailWorkFormDao mMailWorkFormDao;

    @Autowired
    private DataExportDao dataExportDao;

    @Autowired
    private CommonDao commonDao;

    /**
     * 待派-信件工单列表数据
     *
     * @param mmailWorkForm 查询条件
     * @return 列表数据
     */
    @Override
    public List<Map<String, Object>> wfDraftListJson(MmailWorkForm mmailWorkForm) {
        return mMailWorkFormDao.wfDraftListJson(mmailWorkForm);
    }

    /**
     * 待回访信件工单列表数据
     *
     * @param userNo
     * @param mmailWorkForm
     * @return
     */
    @Override
    public List<MmailWorkForm> backWaitListJson(String userNo, MmailWorkForm mmailWorkForm) {
        return mMailWorkFormDao.backWaitListJson(userNo,mmailWorkForm);
    }

    /**
     * 随机获取n条待回访信件工单
     * @param num
     * @return
     */
    @Override
    public List<MmailWorkForm> getBackWaitList(Integer num) {
        return mMailWorkFormDao.getBackWaitList(num);
    }

    /**
     * 未获取信件工单总数
     * @return
     */
    @Override
    public Integer countBackWaitList() {
        return mMailWorkFormDao.countBackWaitList();
    }

    /**
     * 已回访信件工单列表数据
     * @param mmailWorkForm
     * @return
     */
    @Override
    public List<MmailWorkForm> backOverListJson(MmailWorkForm mmailWorkForm) {
        return mMailWorkFormDao.backOverListJson(mmailWorkForm);
    }

    /**
     * 待派-信件工单（信件受理目录下）列表数据
     *
     * @param sysUser       当前用户对象
     * @param mmailWorkForm 查询条件
     * @return 列表数据
     */
    @Override
    public List<Map<String, Object>> wfDraftListWllzJson(SysUser sysUser, MmailWorkForm mmailWorkForm) {
        return getBaseMapper().wfDraftListWllzJson(sysUser.getDeptId().toString(),sysUser.getUserNo(),mmailWorkForm);
    }

    /**
     * 信件工单未回复列表数据
     *
     * @param mailWorkForm 查询条件
     * @param deptId       部门编号
     * @return 列表数据
     */
    @Override
    public List<Map> mailWFNotReplyAllWllzJson(MmailWorkForm mailWorkForm, String deptId) {
        HashMap<String,Object> params = new HashMap<>();
        params.put("deptId",deptId);
        params.put("mail",mailWorkForm);
        return getBaseMapper().mailWFNotReplyAllWllzJson(params);
    }

    /**
     * 信件工单已回复列表数据
     *
     * @param mailWorkForm 查询条件
     * @param deptId       部门编号
     * @return 列表数据
     */
    @Override
    public List<Map> mailWFRepliedAllWllzJson(MmailWorkForm mailWorkForm, String deptId) {
        HashMap<String,Object> params = new HashMap<>();
        params.put("deptId",deptId);
        params.put("mail",mailWorkForm);
        return getBaseMapper().mailWFRepliedAllWllzJson(params);
    }

    /**
     * 查询已删除信件工单 查看页面
     * @param pkId
     * @return
     */
    @Override
    public MmailWorkForm getByPkId(Integer pkId) {
        return mMailWorkFormDao.getByPkId(pkId);
    }

    @Override
    public MmailWorkForm getByWorkFormNO(String workNo) {
        List<MmailWorkForm> list = mMailWorkFormDao.getByWorkFormNO(workNo);
        if(list.size()>0){
            return list.get(0);
        }
        return null;
    }

    /**
     * (网络理政区县）查询本级网络理政件
     * @param sbDeptId 本级部门
     * @param queryConditionVo 参数
     * @return
     */
    @Override
    public List<Map<String, Object>> netMailWFListJson(String sbDeptId, QueryConditionVo queryConditionVo) {
        List<Map<String, Object>> netMailWFList = mMailWorkFormDao.netMailWFList(sbDeptId, queryConditionVo);
        for (Map<String, Object> map : netMailWFList) {
            Map<String, Object> judgeC = commonDao.findMailJudgeByWfId(map.get("WfId").toString());
            if (judgeC != null){
                map.putAll(judgeC);
            }
        }
        return netMailWFList;
    }

    /**
     * (网络理政区县）查询本单位信件工单导出
     * @param sbDeptId 本级部门
     * @param queryConditionVo 参数
     * @return
     */
    @Override
    public List<MailExcel> netMailWFListExport(String sbDeptId, QueryConditionVo queryConditionVo) {
        return dataExportDao.netMailWFListExport(MapDataUtil.multiTypeParamListToMap(queryConditionVo, sbDeptId));
    }
}




